#include <benchmark/benchmark.h>
#include <map>
#include <string>

struct Payload {
    std::string s;
    explicit Payload(std::string x) : s(std::move(x)) {}
};

static void MapInsert(benchmark::State& state) {
    for (auto _ : state) {
        std::map<int, Payload> m;
        for (int i = 0; i < state.range(0); ++i) {
            Payload p(std::to_string(i));
            m.insert(std::make_pair(i, p)); // 复制 pair & Payload
        }
        benchmark::ClobberMemory();
    }
}
BENCHMARK(MapInsert)->RangeMultiplier(2)->Range(64, 1 << 15);

static void MapEmplace(benchmark::State& state) {
    for (auto _ : state) {
        std::map<int, Payload> m;
        for (int i = 0; i < state.range(0); ++i) {
            m.emplace(i, std::to_string(i)); // 原位构造键值
        }
        benchmark::ClobberMemory();
    }
}
BENCHMARK(MapEmplace)->RangeMultiplier(2)->Range(64, 1 << 15);

BENCHMARK_MAIN();